package cn.edu.sdjzu.xg.eagle.exception;

import lombok.Getter;

/**
 * 基础运行时异常类
 *
 * @author STG
 * @project xg22
 * @date 2024/3/27
 */
@Getter
public class BaseException extends RuntimeException {
    private static final long serialVersionUID = 1L;

    /**
     * 所属模块
     */
    private final String module;

    /**
     * 错误码
     */
    private final String code;

    /**
     * 错误码对应的参数
     */
    private final Object[] args;

    /**
     * 错误消息
     */
    private final String defaultMessage;

    /**
     * 原始异常
     */
    private final Exception originalException;

    /**
     * 默认构造器（为子类提供）
     */
    public BaseException() {
        this(null, null, null, null, null);
    }

    // 其他构造方法保持不变
    public BaseException(String defaultMessage) {
        this(null, null, null, defaultMessage, null);
    }

    public BaseException(String module, String defaultMessage) {
        this(module, null, null, defaultMessage, null);
    }

    public BaseException(String module, Exception originalException) {
        this(module, null, null, null, originalException);
    }

    public BaseException(String code, Object[] args) {
        this(null, code, args, null, null);
    }

    public BaseException(String module, String code, Object[] args) {
        this(module, code, args, null, null);
    }

    public BaseException(String module, String code, Object[] args,
                         String defaultMessage, Exception originalException) {
        super(defaultMessage);
        this.module = module;
        this.code = code;
        this.args = args;
        this.defaultMessage = defaultMessage;
        this.originalException = originalException;
    }

    @Override
    public String getMessage() {
        return defaultMessage != null ? defaultMessage :
                (originalException != null ? originalException.getMessage() : super.getMessage());
    }
}